﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Proggy.Web.Cms.PropertyTypes;
using System.Web.Mvc;
using Proggy.Web.Cms.ContentModel;
using Proggy.Web.Cms.Core.Configuration.ConfigObjects;
using System.Web;
using Newtonsoft.Json;

namespace Proggy.Web.Cms.Core.PropertyTypes
{
    /// <summary>
    /// Rich text content property type
    /// </summary>

    [PropertyType("Rich text content editor", "~/Areas/ProggyCore/Views/EditorPartials/RichTextContentEditor.cshtml", RequiredConfigObjectType = typeof(RichTextContentConfigObject))]
    public class RichTextContent : ConfigurablePropertyTypeBase, IValidatablePropertyType, IAutoRenderPropertyType
    {
        /// <summary>
        /// The value of the rich text
        /// </summary>
        [DataType(DataType.MultilineText)]
        public string SavedValue { get; set; }

        /// <summary>
        /// Checks if property is valid based on values and validation context
        /// </summary>
        /// <param name="vContext"></param>
        /// <returns></returns>
        public Tuple<bool, string> GetIsValid(ValidationContext vContext, ContentModelPropertyTypeAttribute attr)
        {
            // check value
            if (SavedValue == HttpUtility.HtmlEncode("<p>&nbsp;</p>") || string.IsNullOrWhiteSpace(SavedValue) || string.IsNullOrEmpty(SavedValue)) {
                return new Tuple<bool, string>(false, "Content area cannot be empty!!");
            }

            // ok
            return new Tuple<bool, string>(true, null);
        }


        public object GetAutoRenderModel()
        {
            return this.SavedValue;
        }

        [JsonIgnore]
        public string FullPathToAutoRenderPartial
        {
            get { return "~/Areas/ProggyCore/Views/AutoRenderPartials/_Rte.cshtml"; }
        }
    }
}
